﻿//AcademicPlanner - course registration planning web app.
//Copyright (C) 2009  Boubacar Diallo

//This program is free software; you can redistribute it and/or
//modify it under the terms of the GNU General Public License
//as published by the Free Software Foundation; either version 2
//of the License, or (at your option) any later version.

//This program is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//GNU General Public License for more details.

//You should have received a copy of the GNU General Public License
//along with this program; if not, write to the Free Software
//Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Hosting;
using System.Web.Mvc;

namespace AcademicPlanner.Framework.Helpers
{
	/// <summary>
	/// UrlHelperExtensions provides static helper methods for
	/// creating urls to pages and resources of the application.
	/// The methods are extensions to the ASP.NET MVC <c>UrlHelper</c>.
	/// </summary>
	/// <since>1.0</since>
	public static class UrlHelperExtensions
	{
		/// <summary>
		/// Gets the url to a css file based on the theme currently set
		/// in the session.
		/// </summary>
		/// <param name="urler">The ASP.NET MVC <c>UrlHelper</c> being extended.</param>
		/// <param name="stylesheetName">The css file name.</param>
		/// <returns>The complete url to the css file.</returns>
		/// <since>1.0</since>
		public static string Css(this UrlHelper urler, string stylesheetName)
		{
			string rUrl = "";
			string vTheme = (string)SessionHelper.Get("Presentation.Theme");
			var vBaseUri = (string)ConfigHelper.GetPresentationSectionValue("urlSettings", "baseUri");
			
			if (!string.IsNullOrEmpty(vTheme) 
				&& HostingEnvironment.VirtualPathProvider.FileExists("~/Content/Themes/" + 
						vTheme + "/CSS/" + stylesheetName + ".css"))
			{
				rUrl = "/Content/Themes/" + vTheme + "/CSS/" + stylesheetName + ".css";
			}
			else
			{
				rUrl = "/Content/CSS/" + stylesheetName + ".css";
			}

			return vBaseUri + rUrl;
		}

		/// <summary>
		/// Gets the url to a small icon.
		/// </summary>
		/// <remarks>
		/// This method simply calls <see cref="M:Icon(UrlHelper, string)"/>
		/// with the provided <paramref name="iconName"/> preprended
		/// with "Small/".
		/// </remarks>
		/// <param name="urler"></param>
		/// <param name="iconName"></param>
		/// <returns>The complete url to the small icon.</returns>
		/// <since>1.0</since>
		public static string SmallIcon(this UrlHelper urler, string iconName)
		{
			return Icon(urler, "Small/" + iconName);
		}

		/// <summary>
		/// Gets the url to a regular icon.
		/// </summary>
		/// <remarks>
		/// This method simply calls <see cref="M:Icon(string)"/>.
		/// </remarks>
		/// <param name="urler">The ASP.NET MVC <c>UrlHelper</c> being extended.</param>
		/// <param name="iconName">The name of the icon image file.</param>
		/// <returns>The complete url to the icon.</returns>
		/// <since>1.0</since>
		public static string Icon(this UrlHelper urler, string iconName)
		{
			return Icon(iconName);
		}

		/// <summary>
		/// Provides the url to an icon based on the current session theme.
		/// </summary>
		/// <remarks>
		/// This method simply calls <see cref="M:Image(string)"/>
		/// with the provided <paramref name="iconName"/> preprended
		/// with "Icons/".
		/// </remarks>
		/// <param name="iconName">The name of the icon image file.</param>
		/// <returns>The complete url to the icon.</returns>
		/// <since>1.0</since>
		public static string Icon(string iconName)
		{
			return Image("Icons/" + iconName);
		}

		/// <summary>
		/// Gets the url to an image based on the current session theme.
		/// </summary>
		/// <remarks>
		/// This method relies on <see cref="M:Image(string)"/>.
		/// </remarks>
		/// <param name="urler">The ASP.NET MVC <c>UrlHelper</c> being extended.</param>
		/// <param name="imageName">The name of the image file.</param>
		/// <returns>The complete url to the image.</returns>
		/// <since>1.0</since>
		public static string Image(this UrlHelper urler, string imageName)
		{
			return Image(imageName);
		}

		/// <summary>
		/// Gets the url to an image based on the current session theme.
		/// </summary>
		/// <param name="imageName">The name of the image file.</param>
		/// <returns>The complete url to the image.</returns>
		/// <since>1.0</since>
		public static string Image(string imageName)
		{
			string rUrl = "";
			string vTheme = (string)SessionHelper.Get("Presentation.Theme");
			var vBaseUri = (string)ConfigHelper.GetPresentationSectionValue("urlSettings", "baseUri");

			if (!string.IsNullOrEmpty(vTheme)
				&& HostingEnvironment.VirtualPathProvider.FileExists("~/Content/Themes/" +
						vTheme + "/Images/" + imageName))
			{
				rUrl = "/Content/Themes/" + vTheme + "/Images/" + imageName;
			}
			else
			{
				rUrl = "/Content/Images/" + imageName;
			}

			return vBaseUri + rUrl;
		}

		/// <summary>
		/// Gets the url to a js file.
		/// </summary>
		/// <param name="urler">The ASP.NET MVC <c>UrlHelper</c> being extended.</param>
		/// <param name="scriptName">The js file name.</param>
		/// <returns>The complete url to the js file.</returns>
		/// <since>1.0</since>
		public static string Js(this UrlHelper urler, string scriptName)
		{
			var vBaseUri = (string)ConfigHelper.GetPresentationSectionValue("urlSettings", "baseUri");
			return vBaseUri + "/Scripts/" + scriptName + ".js";
		}

	}
}
